package ca.eloas.judging.rest.resources;

import ca.eloas.judging.messages.TeamListMessage;
import ca.eloas.judging.messages.TeamMessage;
import ca.eloas.judging.model.TeamObject;
import ca.eloas.judging.objectify.PersistenceService;
import ca.eloas.restsupport.ToMessageOperation;
import ca.eloas.restsupport.utils.BasicResource;
import com.googlecode.objectify.Objectify;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

/**
 * @author JP
 */
@Path("/teams")
public class TeamResource extends BasicResource {

    @Inject
    PersistenceService<Objectify> persistence;

    @GET
    @Path("/")
    public TeamListMessage listTeams() {

        return list(persistence.manager().load().type(TeamObject.class).iterable(), TeamListMessage.class, TeamMessage.class)
                .runOnItems(new ToMessageOperation<TeamObject, TeamMessage>() {
                public void run(TeamObject object, TeamMessage competition) {

                    competition.setName(object.getName());
                }
            }).now();
    }
}
